/**
 *每一行的数据放在tr上的，可以用table_id得到
 *每一个td中都有一个div,都有相同的name,不同的id,
 *第一个都由一个checkdbox,name都是有规则的
 *
 *
 */
(function($) {

	//组件加载数据，只是单纯的加载数据
	function loadPageContent(msg,table_title,tbodyId,opts){
		if(table_title&&table_title.length>0&&tbodyId&&tbodyId.length>0){  
			 
			if(msg&&msg[0]&&msg[0]!="null" && msg[0]!=null){
			 
					var tbody="";
					var tfile = $("#"+table_title).children();
					if(tfile.length<=0){
						//alert("无数据列");
						return;
					}
					$("#"+tbodyId).html("");
			 
					
					for(var i=0;i<msg[0].list.length;i++){
						 
						var vb = msg[0].list[i];
						var tr= '<tr name="'+tbodyId+'_tr" id="'+vb.table_id+'" trId='+vb.table_id+'>';
						 tr+='<td name="'+tbodyId+"_tr_td_"+(i+1)+'">'+(i+1)+'</td>';
						 if(opts.checkbox==true){
							  tr+='<td width="40"><input type="checkbox" name="'+tbodyId+'_checkbox" id="'+tbodyId+'_checkbox"></td>';
						 }
						 for(var j=0;j<tfile.length;j++){
								for(var tc in vb){
									if($(tfile[j]).attr('fname')==tc){
										var width = $(tfile[j]).width();
										if(tc=="showTime"){
											tr+='<td name="'+tbodyId+"_tr_td_"+(i+1)+'">'+vb[tc].substring(0,10)+'</td>';
										}else{
											tr+='<td width="'+width+'"><div id="'+tbodyId+"_"+tc+"_tr_td_"+(i+1)+'" name="'+tbodyId+"_"+tc+"_tr_td"+'" style="width:'+width+'px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;" title="'+vb[tc]+'">'+vb[tc]+'</div></td>';
										}

									}
								}
						 }
						tr+='</tr>';
						//tbody+=tr; 
						$("#"+tbodyId).append(tr);
						$("tr[trId="+vb.table_id+"]").data(vb.table_id,vb);
					} 
				}
			}
	};
	//点击事件 li点击事件
	function LiLister(event){
				var msg =event.data[0];
				var pagination=event.data[1];
				var data=event.data[2];
				var opts=event.data[3];
				var tvalue=$(this).text();
				$("#"+pagination).find(".active").toggleClass("active");
				$(this).toggleClass("active");
				if(tvalue&&tvalue=="<<"){
					loadPage(msg,pagination,"<<",data,opts);
					return;
				}
				if(tvalue&&tvalue=="<"){
					loadPage(msg,pagination,"<",data,opts);
					return;
				}
				if(tvalue&&tvalue==">"){
					loadPage(msg,pagination,">",data,opts);
					return;
				}
				if(tvalue&&tvalue==">>"){
					loadPage(msg,pagination,">>",data,opts);
					return;
				}
				if(tvalue&&tvalue=="..."){
					return;
				}
				if($(this).text() && $(this).text().length>6){
					return;
				}
				opts.data.start=$(this).text();
				
				loadPageData(opts);

	};
	function loadPageData(opts){
		 $.ajax({
		   type: "POST",
		   url: opts.url,
		   data:opts.data,
		   dataType:"json",
		   success: function(msg){ 
			      
				 loadPageContent(msg,opts.table_title,opts.tbodyId,opts);
				 //执行成功的方法
				 opts.success();
				 //findText(opts.data.start,opts.pagination);
				 //选中已经有page
				 //loadPage(msg,opts.pagination,"<<",opts.data,opts);
		   }
		});
	}
	//给丛新加事件的li加css选中效果
	function findText(flag,pagination){
					for(var i=0;i< $("#"+pagination).children().length;i++){
						var ab = $("#"+pagination).children()[i];
						if($(ab).text()==flag){
								$("#"+pagination).find(".active").toggleClass("active");
								$(ab).toggleClass("active");
								break;
						}
					}
	};

	//生成分页
	function loadPage(msg,pagination,flag,data,opts){
				 if(msg=="null" || msg[0]=="null" || msg[0]==null){
			    	 return;
			     }
				var allPage =msg[0].count%7==0?parseInt(msg[0].count/7):parseInt(msg[0].count/7+1);
				var mypage =parseInt($("#"+pagination).attr("cpage"));
			    if(!mypage){
			    	mypage=1;
			    }
				var ULContent="";
				if(flag==""|| flag=="<<"){
						   $("#"+pagination).attr("cpage",mypage);
							ULContent ='<li f="1"><a href="javascript:;"" value="1" id="'+pagination+'pali1"><<</a></li>';
							ULContent +='<li f="1"><a href="javascript:;"" value="up" id="'+pagination+'pali2"><</a></li>';
							for(var i=0;i<msg[0].count;i++){
							  ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali3">'+(i+1)+'</a></li>';
							  if(i>5){
								   ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali4">...</a></li>';
								   break;
							  }
							}
							ULContent+='<li f="1"><a href="javascript:;"" value="down">></a></li>';
							ULContent+='<li f="1" class="active"><a href="javascript:;" value="1">>></a></li>';
							ULContent+='<li f="1" class="disabled"><a href="javascript:;">当前查询共【'+msg[0].sum+'】条数据</a></li>';
							$("#"+pagination).html(ULContent);//改入页面
							$("#"+pagination).children().click([msg,pagination,data,opts],LiLister);
							 findText(flag,pagination);
							//loadAjaxData(mypage,limit);
							return;
				}
				//前一页
				if(flag=="<"){
					mypage=parseInt(mypage)-1;
					if(mypage<=0){
						$("#"+pagination).attr("cpage",1);
						mypage=1;
					}else{
						$("#"+pagination).attr("cpage",mypage);
					}
				}
				//后一页
				if(flag==">"){
					mypage=parseInt(mypage)+1;

					if(mypage>=allPage){
						$("#"+pagination).attr("cpage",allPage);
						mypage=allPage;
					}else{
						$("#"+pagination).attr("cpage",mypage);
					}
				}
				//最后一页
				if(flag==">>"){
					mypage=allPage;
					$("#"+pagination).attr("cpage",mypage);
				}
				//如果当前的页
				if(mypage>=allPage){
						ULContent ='<li f="1"><a href="javascript:;"" value="1" id="'+pagination+'pali1"><<</a></li>';
						ULContent +='<li f="1"><a href="javascript:;"" value="up" id="'+pagination+'pali2"><</a></li>';
						var temp=0;
						for(var i=msg[0].count-7;i<msg[0].count;i++){
							temp++;
						  if(i<0){
							 continue;
						  }
						  ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali3">'+(i+1)+'</a></li>';
						  if(temp>7){
							   ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali4">...</a></li>';
							   break;
						  }
						}
					  ULContent+='<li f="1"><a href="javascript:;"" value="down">></a></li>';
					  ULContent+='<li f="1" class="active"><a href="javascript:;" value="1">>></a></li>';
					  ULContent+='<li f="1" class="disabled"><a href="javascript:;">当前查询共【'+msg[0].sum+'】条数据</a></li>';
					  $("#"+pagination).html(ULContent);//改入页面
					 $("#"+pagination).children().click([msg,pagination,data,opts],LiLister);
					 findText(flag,pagination);
					 //loadAjaxData(mypage,limit);
					 return;
				}
				if(mypage<allPage){
					    ULContent ='<li f="1"><a href="javascript:;"" value="1" id="'+pagination+'pali1"><<</a></li>';
						ULContent +='<li f="1"><a href="javascript:;"" value="up" id="'+pagination+'pali2"><</a></li>';
						for(var i=(mypage-1)*7;i<(mypage-1)*7+7;i++){
						  ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali3">'+(i+1)+'</a></li>';
						  if(i>(mypage-1)*7+5){
							   ULContent+='<li ><a href="javascript:;" name="'+pagination+'pali'+'" id="'+pagination+'pali4">...</a></li>';
							   break;
						  }
						}
					  ULContent+='<li f="1"><a href="javascript:;"" value="down">></a></li>';
					  ULContent+='<li f="1" class="active"><a href="javascript:;" value="1">>></a></li>';
					  ULContent+='<li f="1" class="disabled"><a href="javascript:;">当前查询共【'+msg[0].sum+'】条数据</a></li>';
					 $("#"+pagination).html(ULContent);//改入页面
					 $("#"+pagination).children().click([msg,pagination,data,opts],LiLister);
					 findText(flag,pagination);
					 //loadAjaxData(mypage,limit);
					 return;
				}
	};
	//初始化
	function init(_this,opts) {
		 $.ajax({
		   type: "POST",
		   url: opts.url,
		   data:opts.data,
		   dataType:"json",
		   success: function(msg){
			     if(msg=="null" || msg[0]=="null"){
			    	 return;
			     }
				 loadPageContent(msg,opts.table_title,opts.tbodyId,opts);
				 opts.success();
				 loadPage(msg,opts.pagination,"<<",opts.data,opts);
				 findText(opts.data.start,opts.pagination);

		   }
		});
	};
	//组件初始化
	$.fn.wgrid = function(method, options) {
		if (typeof (method) == "string") {

			return $.fn.wgrid.methods[method](this, options);
		} else {
			var opts = $.extend({},$.fn.wgrid.defaults,method);
			init(this,opts);
		}
	};
	//组件默认参数
	$.fn.wgrid.defaults = {
		table_title:"",
		tbodyId:"",
		pagination:"",
		checkbox:false,
		url:"",
		data:{start:1,limit:20},
		success:function(){
			//alert("d");
		}

	};
	//组件方法注册
	$.fn.wgrid.methods = {
		init : function(_this, opts) {
			init(_this,opts);
		}
	};
})(jQuery);
;